package com.hpe.aruba.CPPM.objects;

import android.os.Parcel;
import android.os.Parcelable;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;

public class Device implements Parcelable, Serializable {
    /*This object is for devices AND endpoints.*/
    private int id;
    private String mac;
    private String vendor;
    private int occurrences = 1;
    private String create_time;
    private boolean enabled = true;
    private String expire_time;
    private boolean is_endpoint = false;
    private String last_known_location;
    private String notes; /*"description" for endpoints, "notes" for devices*/
    private int role_id;
    private String role_name;
    private String source;
    private String sponsor_name;
    private String sponsor_profile;
    private String sponsor_profile_name;
    private String status;
    private String start_time;
    private long updated_at; /*updated_at from insight, in milliseconds*/
    private String visitor_name;
    private boolean imported;
    private String name;

    public JSONObject toJson() {
        JSONObject object = new JSONObject();
        try {
            object.put("username", name);
            object.put("mac", mac);
            object.put("vendor", vendor);
            object.put("enabled", enabled);
            object.put("expire_time", expire_time);
            object.put("create_time", create_time);
            object.put("notes", notes);
            object.put("role_id", role_id); //TODO change
            object.put("role_name", role_name);
            object.put("source", source);
            object.put("sponsor_name", sponsor_name);
            object.put("sponsor_profile", sponsor_name);
            object.put("sponsor_profile_name", sponsor_profile_name);
            object.put("start_time", start_time);
            object.put("status", status);
            object.put("visitor_name", visitor_name);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return object;
    }

    public String getCreate_time() {
        return create_time;
    }

    public void setCreate_time(String create_time) {
        this.create_time = create_time;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public String getExpire_time() {
        return expire_time;
    }

    public void setExpire_time(String expire_time) {
        this.expire_time = expire_time;
    }

    public boolean isEndpoint() {
        return is_endpoint;
    }

    public void setEndpoint(boolean is_endpoint) {
        this.is_endpoint = is_endpoint;
    }

    public String getLast_known_location() {
        return last_known_location;
    }

    public void setLast_known_location(String last_known_location) {
        this.last_known_location = last_known_location;
    }

    public String getNotes() {
        return notes;
    }

    public void setNotes(String notes) { this.notes = notes; }

    public int getRole_id() {
        return role_id;
    }

    public void setRole_id(int role_id) {
        this.role_id = role_id;
    }

    public String getRole_name() {
        return role_name;
    }

    public void setRole_name(String role_name) {
        this.role_name = role_name;
    }

    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public String getSponsor_name() {
        return sponsor_name;
    }

    public void setSponsor_name(String sponsor_name) {
        this.sponsor_name = sponsor_name;
    }

    public String getSponsor_profile() {
        return sponsor_profile;
    }

    public void setSponsor_profile(String sponsor_profile) {
        this.sponsor_profile = sponsor_profile;
    }

    public String getSponsor_profile_name() {
        return sponsor_profile_name;
    }

    public void setSponsor_profile_name(String sponsor_profile_name) {
        this.sponsor_profile_name = sponsor_profile_name;
    }

    public String getStart_time() {
        return start_time;
    }

    public void setStart_time(String start_time) {
        this.start_time = start_time;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public long getUpdated_at() {
        return updated_at;
    }

    public void setUpdated_at(long updated_at) {
        this.updated_at = updated_at;
    }

    public String getVisitor_name() {
        return visitor_name;
    }

    public void setVisitor_name(String visitor_name) {
        this.visitor_name = visitor_name;
    }


    public Device() {}

    public Device(String mac) {
        this.mac = mac;
        create_time = "" + System.currentTimeMillis();
    }

    public Device(String mac, String vendor) {
        create_time = "" + System.currentTimeMillis();
        this.mac = mac;
        this.vendor = vendor;
    }

    protected Device(Parcel in) {
        String[] data = new String[6];
        in.readStringArray(data);
        mac = data[0];
        vendor = data[1];
        create_time = data[2];
        imported = Boolean.parseBoolean(data[3]);
        role_id = Integer.parseInt(data[4]);
        role_name = data[5];
    }

    public static final Creator<Device> CREATOR = new Creator<Device>() {
        @Override
        public Device createFromParcel(Parcel in) {
            return new Device(in);
        }

        @Override
        public Device[] newArray(int size) {
            return new Device[size];
        }
    };

    public int getOccurrences() {
        return occurrences;
    }

    public void setOccurrences(int occurrences) {
        this.occurrences = occurrences;
    }

    public String getMac() {
        return mac;
    }

    public void setMac(String mac) {
        this.mac = mac;
    }

    public void occurrenceFound() {
        occurrences++;
    }

    public String getVendor() {
        return vendor;
    }

    public void setVendor(String vendor) {
        this.vendor = vendor;
    }

    public long getCreation() {
        try{
        return Long.parseLong(create_time);
        } catch(Exception e){
            return 0L;
        }
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeStringArray(new String[]{this.mac, this.vendor, create_time, "" + imported, "" + role_id, role_name});
    }

    public boolean isImported() {
        return imported;
    }

    public void setImported(boolean imported) {
        this.imported = imported;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Device{" +
                "id=" + id +
                ", mac='" + mac + '\'' +
                ", vendor='" + vendor + '\'' +
                ", occurrences=" + occurrences +
                ", create_time='" + create_time + '\'' +
                ", status='" + status + '\'' +
                ", enabled=" + enabled +
                ", expire_time='" + expire_time + '\'' +
                ", is_endpoint='" + is_endpoint + '\'' +
                ", notes='" + notes + '\'' +
                ", role_id=" + role_id +
                ", role_name='" + role_name + '\'' +
                ", source='" + source + '\'' +
                ", sponsor_name='" + sponsor_name + '\'' +
                ", sponsor_profile='" + sponsor_profile + '\'' +
                ", sponsor_profile_name='" + sponsor_profile_name + '\'' +
                ", start_time='" + start_time + '\'' +
                ", status='" + status + '\'' +
                ", visitor_name='" + visitor_name + '\'' +
                ", imported=" + imported +
                ", name='" + name + '\'' +
                '}';
    }
}
